package com.cloudkinto.controller.ft;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.vo.ft.*;
import com.cloudkinto.vo.ft.importVo.FreightWeightImportRes;
import com.cloudkinto.vo.channel.lc.LcChannelPageRes;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.ft.FreightTempService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 物流费模板 前端控制器
 * </p>
 * @author hua
 * @since 2024-11-07
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "物流费模板omp模块")
@RequestMapping("/api/admin/omp/FreightTemp")
public class FreightTempController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FreightTempController.class);

    private final FreightTempService service;


    @UserSessionRequired
    @PostMapping
    @ApiOperation(value = "物流费模板新增", notes = "物流费模板新增")
    public SingleResult add(@RequestBody @Validated FreightTempAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功",service.addFreightTemp(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @PostMapping("/addFreight")
    @ApiOperation(value = "物流费新增", notes = "物流费新增")
    public SingleResult addFreight(@RequestBody @Validated FreightAddReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("新增成功",service.addFreight(req,userId, getTenantId()));
    }


    @UserSessionRequired
    @PutMapping
    @ApiOperation(value = "物流费模板修改", notes = "物流费模板修改")
    public SingleResult update(@RequestBody @Validated FreightTempUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateFreightTemp(req, userId, getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("/updateFreight")
    @ApiOperation(value = "物流费修改", notes = "物流费修改")
    public SingleResult updateFreight(@RequestBody @Validated FreightUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.updateFreight(req, userId, getTenantId()));
    }


    @UserSessionRequired
    @PutMapping("/updateStatus")
    @ApiOperation(value = "物流费停用启用", notes = "物流费停用启用")
    public SingleResult updateStatus(@RequestBody @Validated FreightStatusUpdateReq req) {
        Long userId = getCurrentUserId();
        service.updateStatus(req, userId, getTenantId());
        return SingleResult.success();
    }


    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "物流费模板根据id删除", notes = "物流费模板根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id, userId, getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "物流费模板或物流费根据id查看详情", notes = "物流费模板根据id查看详情")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FreightTempDetailRes.class))
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功",service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "物流费模板查询初始化", notes = "物流费模板查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }


    @UserSessionRequired
    @GetMapping("/getChannelList")
    @ApiOperation(value = "物流渠道下拉框查询", notes = "物流渠道下拉框查询")
    @ApiImplicitParams({
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = LcChannelPageRes.class))
    public SingleResult getChannelList(HttpServletRequest request){
        return SingleResult.success("查询成功", service.getChannelList(getTenantId()));
    }

    @UserSessionRequired
    @GetMapping("/list")
    @ApiOperation(value = "物流费模板查询", notes = "物流费模板查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
            @ApiImplicitParam(name = "type", value = "类型 物流费 freight 还是物流费模板 freightTemp"),
            @ApiImplicitParam(name = "freightTempName", value = "物流费模板名称"),
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FreightTempPageRes.class))
    public SingleResult list(HttpServletRequest request){

        Map<String, Object> map = new HashMap<>();
        String freightTempName = request.getParameter("freightTempName");
        String currencyCode = request.getParameter("currencyCode");
        String type = request.getParameter("type");
        map.put(currencyCode, currencyCode);
        map.put("tenantId", getTenantId());
        map.put("type", type);
        map.put("freightTempName", freightTempName);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小"),
            @ApiImplicitParam(name = "currencyCode", value = "币种"),
            @ApiImplicitParam(name = "type", value = "类型 物流费 freight 还是物流费模板 freightTemp"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "channelId", value = "物流渠道id"),
            @ApiImplicitParam(name = "freightTempName", value = "物流费模板名称"),
            @ApiImplicitParam(name = "sortColumn", value = "排序列"),
            @ApiImplicitParam(name = "sort", value = "排序"),
    })
    @ApiOperation(value = "物流费模板查询（分页）", notes = "物流费模板查询（分页）")
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FreightTempPageRes.class))
    public PageResult listPage(HttpServletRequest request){

        Map<String, Object> map = new HashMap<>();
        String currencyCode = request.getParameter("currencyCode");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String freightTempName = request.getParameter("freightTempName");
        String sortColumn = request.getParameter("sortColumn");
        String sort = request.getParameter("sort");
        String type = request.getParameter("type");
        String status = request.getParameter("status");
        String channelId = request.getParameter("channelId");

        map.put("currencyCode", currencyCode);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("tenantId", getTenantId());
        map.put("freightTempName", freightTempName);
        map.put("sortColumn", sortColumn);
        map.put("sort", sort);
        map.put("type", type);
        map.put("status", status);
        map.put("channelId", channelId);
        return service.getListPage(map);
    }



    @UserSessionRequired
    @GetMapping("/getFullTemplate")
    @ApiOperation(value = "下载重量段模板", notes = "")
    @ApiImplicitParams({@ApiImplicitParam(name = "volumeWeightCalculateWay", value = "体积重"),
            @ApiImplicitParam(name = "zone", value = "分区")
    })
    public void getFullTemplate(HttpServletRequest request, HttpServletResponse response) {
        String volumeWeightCalculateWay = request.getParameter("volumeWeightCalculateWay");
        service.getFullTemplate(StringUtils.isBlank(volumeWeightCalculateWay) ? null : Integer.parseInt(volumeWeightCalculateWay), request.getParameter("zone"), response);
    }



    @UserSessionRequired
    @PostMapping("/importT")
    @ApiOperation(value = "导入重量段价格")
    @ApiImplicitParams({@ApiImplicitParam(name = "volumeWeightCalculateWay", value = "体积重"),
            @ApiImplicitParam(name = "zone", value = "分区")
    })
    @ApiResponses(@ApiResponse(code = 200, message = "", response = FreightWeightImportRes.class))
    public SingleResult importT(MultipartFile file, HttpServletRequest request) {
        String volumeWeightCalculateWay = request.getParameter("volumeWeightCalculateWay");
        FreightWeightImportRes res = service.importT(file, StringUtils.isBlank(volumeWeightCalculateWay) ? null : Integer.parseInt(volumeWeightCalculateWay), request.getParameter("zone"));
        return SingleResult.success(res);
    }



}

